/*-
 * Copyright (c) 2006 Vishal Patil <bsd.devil@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#ifndef _ISCSI_UTIL_H_
#define _ISCSI_UTIL_H_

#include "iscsi_pdu.h"

#define MIN(x,y) (((x) < (y))? (x):(y))
#define MAX(x,y) (((x) > (y))? (x):(y))

/* Convert a three byte array to a value */
#define ntoh24(a) ((a)[2] | ((a)[1] << 8) | ((a)[0] << 16))

/* assumes a pointer to a 3 byte array, and an integer value */
#define hton24(a, v) {\
        a[0] = (((v) >> 16) & 0xFF); \
        a[1] = (((v) >> 8) & 0xFF); \
        a[2] = ((v) & 0xFF); \
}

/* Integer to string*/
#define itoa(buffer,value) {\
	memset((buffer),0,sizeof((buffer)));\
	sprintf((buffer),"%d",(value));\
}	
				    
/* 	Adds key val to the data segment of iSCSI PDU */
int
add_key_value(struct iscsi_pdu_incore * pdu, const char * key,
		                        const char * value );

/*
 *  	Try searching for a key in the given data segment. If found     
 *  	return the value along with the length of the value. 
 */
int
get_key_value(const char * option_key, struct iscsi_pdu_incore *pdu, 
		char * option_value);

/*
 *  	Generate an incore PDU from the received buffer. The incore PDU is
 *  	obtained from the pdu pool
 */
struct iscsi_pdu_incore * 
buffer_to_pdu(uint8_t * buffer, uint32_t buffer_size); 

/*
 *      Copy the incore pdu to a data buffer and retun the PDU back to
 *      the PDU pool.
 */
int
pdu_to_buffer(struct iscsi_pdu_incore * pdu, uint8_t * buffer,
		                int buffer_size); 

#endif
